package vn.gq.tym.view;

import vn.gq.tym.tonghopvf.R;
import vn.gq.tym.tonghopvf.ReaderSettings;
import vn.gq.tym.utils.Client;
import vn.gq.tym.utils.DataType;
import vn.gq.tym.utils.IClientListener;
import vn.gq.tym.utils.IScrollListener;
import vn.gq.tym.widget.BaseFragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.text.Html;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

public class ReaderFragment extends BaseFragment implements OnTouchListener {

	private String mChapterPath;
	private ScrollView mScrollView;
	private TextView mTextView;
	private FrameLayout readerLayout;
	private int mScrollPosY = 0;
	private ProgressBar mProgressBar;
	private GestureDetectorCompat mDetector;
	private int mPosition;
	
	public static ReaderFragment newInstance(int position, String filePath, int scrollPos) {
		ReaderFragment f = new ReaderFragment();
		Bundle args = new Bundle();
		args.putString("PATH", filePath);
		args.putInt("SCROLLPOS", scrollPos);
		args.putInt("POSITION", position);
		f.setArguments(args);
		return f;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			mChapterPath = getArguments().getString("PATH");
			mScrollPosY = getArguments().getInt("SCROLLPOS");
			mPosition = getArguments().getInt("POSITION");
		}
		setRetainInstance(true);
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
	}

	@Override
	public void onResume() {
		super.onResume();
		if (getActivity() instanceof OnGestureListener && getActivity() instanceof OnDoubleTapListener) {
			mDetector = new GestureDetectorCompat(getActivity(), (OnGestureListener) getActivity());
			mDetector.setOnDoubleTapListener((OnDoubleTapListener) getActivity());
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public int getLayoutId() {
		return R.layout.fragment_reader;
	}

	@Override
	public int getContainerId() {
		return 0;
	}
	
	public void setNightMode() {
		readerLayout.setBackgroundResource(R.drawable.abs__ab_solid_dark_holo);
		mScrollView.setBackgroundResource(R.drawable.abs__ab_solid_dark_holo);
		mTextView.setBackgroundResource(R.drawable.abs__ab_solid_dark_holo);
		mTextView.setTextColor(Color.WHITE);
	}
	
	public void setLightMode() {
		readerLayout.setBackgroundResource(R.drawable.abs__activated_background_holo_light);
		mScrollView.setBackgroundResource(R.drawable.abs__activated_background_holo_light);
		mTextView.setBackgroundResource(R.drawable.abs__activated_background_holo_light);
		mTextView.setTextColor(getResources().getColor(R.color.light_reader_text_color));
	}

	@Override
	public void createView(Bundle savedInstanceState) {
		ReaderSettings settings = ReaderSettings.getInstance(getActivity());
		readerLayout = (FrameLayout) findViewById(R.id.reader_container);
		mScrollView = (ScrollView) findViewById(R.id.scroll);
		mTextView = (TextView) findViewById(R.id.reader_light_bg);
		mTextView.setTextSize(settings.getFontSize(20));
		if (settings.isNightMode()) 
			setNightMode();
		else
			setLightMode();
		mScrollView.setOnTouchListener(this);
		mProgressBar = (ProgressBar) findViewById(R.id.loading);
		Client client = new Client(getActivity());
		client.setOutputDataType(DataType.JSON);
		client.executeAndListener(getFilePath(), new IClientListener() {
			
			@Override
			public void onDone(int code, Object result) {
				mTextView.setText(Html.fromHtml(String.valueOf(result)), TextView.BufferType.SPANNABLE);
				if (mScrollPosY > 0) {
					mScrollView.post(new Runnable() {
						
						@Override
						public void run() {
							mScrollView.smoothScrollTo(0, mScrollPosY);
						}
					});
				}
				mProgressBar.setVisibility(View.GONE);
			}
		});
		mScrollView.setOnTouchListener(this);
	}

	public String getFilePath() {
		if (!mChapterPath.startsWith("http://")) {
			return String.format("asset://gzip:files/%s", mChapterPath);
		}
		return mChapterPath;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		this.mDetector.onTouchEvent(event);
		mScrollPosY = mScrollView.getScrollY();
		if (mScrollPosY < 0) mScrollPosY = 0;
		if (getActivity() instanceof IScrollListener) {
			((IScrollListener) getActivity()).onScroll(mPosition, 0, mScrollPosY);
		}
		return false;
	}
	
	public void onBackPressed() {
		
	}
	
}
